//  https://www.typescriptlang.org/docs/handbook/module-resolution.html#module-resolution-strategies
import mongoose from "mongoose";

/**
 * 连接到MongoDB
 */
export default async function connectDB() {
  const { MONGO_URL, MONGO_USER, MONGO_PASS } = process.env;
  //   https://mongoosejs.com/docs/connections.html#error-handling
  try {
    await mongoose.connect(MONGO_URL, {
      user: MONGO_USER,
      pass: MONGO_PASS,
    });
    // 当服务器连接首次成功,就监听当前连接上的每个可能的错误，并log
    mongoose.connection.on("error", (err) => {
      // 简单的向控制台报错
      console.log(err);
    });
    // 当服务器连接首次成功，就提示连接成功的log
    console.log("成功连接木地板数据库");
  } catch (error) {
    console.log(" connecting to the 木地板 database发生错误");
    console.log(error);
    process.exit();
  }
}
